<?php


namespace app\zmt\controller;


use app\zmt\model\NavModel;
use app\zmt\validate\NavValidate;
use cmf\controller\AdminBaseController;

class NavController extends AdminBaseController
{
    //显示数据
    public function index(){
        $title = input('title');
        $where = [];
        if($title){
            $where[] = ['title','like','%'.$title.'%'];
        }
        $list = NavModel::order('id', 'DESC')
            ->where($where)
            ->paginate(1);
        return $this->fetch('index',[
            'list' => $list,
        ]);
    }
    //添加
    public function add(){
        //查询数据
        $list = NavModel::select();
        return $this->fetch('add',[
            'list' => $list,
        ]);
    }
    public function addPost(){
        $data = input('post.');
        $validate = new NavValidate();

        $result = $validate->scene('add')->check($data);
        if(true !== $result){
            $this->error($validate->getError());
        }
        //入库
        if(NavModel::create($data)){
            $this->success('添加成功','Nav/index');
        }else{
            $this->error('添加失败');
        }
    }
    //修改
    public function edit(){
        $id = input('id');
        $list = NavModel::find($id);
        return $this->fetch('edit',[
            'list' => $list,
        ]);
    }
    public function editPost(){
        $data = input('post.');
        //验证器
        $validate = new NavValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询是否有此记录
        $list = NavModel::find($data['id']);
        if(!$list){
            $this->error('未查询到该记录');
        }
        if($list->update($data)){
            $this->success('修改成功','Nav/index');
        }else{
            $this->error('修改失败');
        }
    }
//删除
    public function delete(){
        $id = input('id/d','');
        $del = NavModel::destroy($id);
        if($del){
            $this->success('删除成功','Nav/index');
        }else{
            $this->error('删除失败');
        }
    }
}